//dom加载完成后执行的js
$(document).ready(function(){
	/*
	 *清空所有图标 
	 */
	function clearclass(name){
		$("#"+name).next().find('i').css("display","none");
		$("#"+name).next().find('b').css("display","none");
		$("#"+name).next().find('em').css("display","none");
		$("#"+name).next().find('span').removeClass("colorred");
	}
	/*
	 *加载注意图标 
	 */
	function show_attention(name){
		$("#"+name).next().find('i').css("display","none");
		$("#"+name).next().find('b').css("display","none");
		$("#"+name).next().find('em').css("display","none");
		$("#"+name).next().find('i').css("display","block");
		$("#"+name).next().find('span').removeClass("colorred");
	}
	$("#username").focus(function(){
		show_attention("username");
	}).blur(function(){
		var username = $(this).val();
		var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d\w]{4,16}$/;
		var flag;
		flag = reg.test(username);
		if(flag){
			clearclass("username");
			$(this).next().find('b').css("display","block");
		}else{
			clearclass("username");
			$(this).next().find('em').css("display","block");
			$(this).next().find('span').addClass("colorred");
		}
	});
	$("#email").focus(function(){
		show_attention("email");
	}).blur(function(){
		var email = $(this).val();
		var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var flag;
		flag = reg.test(email);
		if(flag){
			clearclass("email");
			$(this).next().find('b').css("display","block");
		}else{
			clearclass("email");
			$(this).next().find('em').css("display","block");
			$(this).next().find('span').addClass("colorred");
		}
	});
	$("#telephone").focus(function(){
		show_attention("telephone");
	}).blur(function(){
		var telephone = $(this).val();
		var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/;
		var flag;
		flag = reg.test(telephone);
		if(flag){
			clearclass("telephone");
			$(this).next().find('b').css("display","block");
		}else{
			clearclass("telephone");
			$(this).next().find('em').css("display","block");
			$(this).next().find('span').addClass("colorred");
		}
	});
	$("#password").focus(function(){
		show_attention("password");
	}).blur(function(){
		var password = $(this).val();
		var reg = /^[a-zA-Z]\w{5,17}$/;
		var flag;
		flag = reg.test(password);
		if(flag){
			clearclass("password");
			$(this).next().find('b').css("display","block");
		}else{
			clearclass("password");
			$(this).next().find('em').css("display","block");
			$(this).next().find('span').addClass("colorred");
		}
	});
	$("#repassword").focus(function(){
		show_attention("repassword");
	}).blur(function(){
		var password = $("#password").val();
		var repassword = $(this).val();
		//alert(password+"@@"+repassword);
		if(password!==repassword || repassword.length == 0){
			clearclass("repassword");
			$(this).next().find('em').css("display","block");
			$(this).next().find('span').addClass("colorred");
			$(this).next().find('span').html('未输入或两次输入密码不同');
		}else{
			clearclass("repassword");
			$(this).next().find('b').css("display","block");
		}
	});
	/*function checkUsername(){
		//alert("nihao");
		var username = $("#username").val();
		var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d\w]{4,16}$/;
		var flag;
		flag = reg.test(username);
		if(flag){
			clearclass("username");
			$("#username").next().find('b').css("display","block");
		}else{
			clearclass("username");
			$("#username").next().find('em').css("display","block");
			$("#username").next().find('span').addClass("colorred");
		}
		return flag;
	}*/
	function check(name,reg){
		var name_value = $("#"+name).val();
		var flag;
		flag = reg.test(name_value);
		if(flag){
			clearclass(name);
			$("#"+name).next().find('b').css("display","block");
		}else{
			clearclass(name);
			$("#"+name).next().find('em').css("display","block");
			$("#"+name).next().find('span').addClass("colorred");
		}
		return flag;
	}
	function checkRepassword(){
		var password = $("#password").val();
		var repassword = $("#repassword").val();
		if(password!==repassword || repassword.length == 0){
			clearclass("repassword");
			$("#repassword").next().find('em').css("display","block");
			$("#repassword").next().find('span').addClass("colorred");
			$("#repassword").next().find('span').html('未输入或两次输入密码不同');
			return false;
		}else{
			return true;
		}
	}
	//ajax post submit请求
	$(".ajax-post").click(function(){
		var target,query,form;
		var target_form = $(this).attr('target-form');
		var that = this;
        var username_flag,email_flag,telephone_flag,password_flag,repassword_flag;
        username_flag = check("username",/^[\u4E00-\u9FA5\uf900-\ufa2d\w]{4,16}$/);
        email_flag = check("email",/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)
        telephone_flag = check("telephone",/^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/);
        password_flag = check("password",/^[a-zA-Z]\w{5,17}$/);
        repassword_flag = checkRepassword();
        
        
        
        /*form = $('.'+target_form);
    	query = form.serialize();
    	//console.log(query);
    	target = form.get(0).action;
    	$.post(target,query,function(data){
    		console.log(data);
    	});*/
        if(username_flag && email_flag && telephone_flag && password_flag &&repassword_flag){
        	form = $('.'+target_form);
        	query = form.serialize();
        	//console.log(query);
        	target = form.get(0).action;
        	$.post(target,query,function(data){
        		if(data.flag == 0){
        			alert(data.content);
        		}else if(data.flag == 1){
        			updateAlert(data.content,true);
        			setTimeout(function(){
    					if(data.url)
    					{
    						location.href=data.url;
    					}
    					else{
    						location.reload();
    					}
    				},1500);
        		}else if(data.flag ==-1){
        			updateAlert(data.content);
        		}
        	},"json");
        }
        /*form = $('.'+target_form);
    	query = form.serialize();
    	//console.log(query);
    	target = form.get(0).action;
    	$.post(target,query,function(data){
    		console.log(data.flag);
    		if(data.flag == 0){
    			alert(data.content);
    		}else if(data.flag == 1){
    			updateAlert(data.content,true);
    			setTimeout(function(){
					if(data.url)
					{
						location.href=data.url;
					}
					else{
						location.reload();
					}
				},1500);
    		}else if(data.flag ==-1){
    			updateAlert(data.content);
    		}
    	},"json");*/
	});
	
	
	
	//警告栏显示
	var am_alert = $('#top-alert');
	var alert_button = am_alert.find('button');
	alert_button.click(function(){
		am_alert.hide();
	});
	window.updateAlert = function(text,c)
	{
		text = text||'default';
		c = c||false;
		am_alert.css("display","block");
		if(text!='default'){
			am_alert.find('p').text(text);
		}
		else{
			am_alert.find('p').text('这是内容!');
		}
		if(c==false)
		{
			am_alert.removeClass("alert-success").addClass(' alert-error');
		}else{
			am_alert.removeClass("alert-error").addClass(" alert-success");
		}
	}
});